package com.zkingsoft.actions.admin.web;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.Authority;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.web.WebArticle;
import com.zkingsoft.model.web.WebArticleType;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.web.WebArticleService;
import com.zkingsoft.services.web.WebArticleTypeService;
import com.zkingsoft.util.WebUtil;

/**
 * @author 陈才
 * @date 2016-12-01 10:36
 */
@Controller
@RequestMapping(value = "admin/webArticle")
public class WebArticleController extends BaseController{

	@Resource
	private WebArticleService webArticleService;
	
	@Resource
	private WebArticleTypeService webArticleTypeService;
	
	@Resource(name="authorityAdapter")
	private Authority  authorityAdapter;
	
	
	
	//记录编辑前的值Before_Edit_Value
	public static final String BEV="WebArticle_BEV";
	
	public static final String fnCode = "WebArticle";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebArticle webArticle, PaginationVO pageVo) {
		authorityAdapter.isBtnPermitted(search);
		return showList(webArticleService, webArticle, pageVo);
	}
   	
	/**
	 * 新增或者修改页面
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(WebArticle webArticle) {
		if (webArticle.getArtId() != null) {
	   		authorityAdapter.isBtnPermitted(edit);
	   		webArticle.setArtId(((WebArticle)WebUtil.getSessionAttribute(BEV)).getArtId());
	   		AjaxResult result=modify(webArticleService, WebUtil.getSessionAttribute(BEV), webArticle, "文章");
			WebUtil.removeSessionAttribute(BEV);
			return  result;
		} else {
			// authorityAdapter.isBtnPermitted(add);
			webArticle.setArtCreatetiem(new Date());
			return add(webArticleService, webArticle, "文章");
		}
	}
	
   	/**
	 * 进入修改界面
	 */   
	@SaveRequestToken
   	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		authorityAdapter.isBtnPermitted(edit);
		WebArticle webArticle;
		if (id != null) {
			webArticle = webArticleService.findById(id);
			WebUtil.getRequest().setAttribute("obj", webArticle);
			WebUtil.setSessionAttribute(BEV, webArticle);
		}
		return "admin/web/article-form";
	}
   	
   	
   	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		authorityAdapter.isBtnPermitted(del);
		return remove(webArticleService, keys);
	}
 	
 	/**
	 * 查询文章列表
	 * 
	 * @param typeId
	 * @param pageVo
	 * @return
	 */
	@RequestMapping(value = "/getNewsListByType")
	@ResponseBody
	public AjaxResult getNewsListByType(Long typeId, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		
		WebArticleType artType = webArticleTypeService.findById(typeId);
		WebUtil.getRequest().setAttribute("typeName", artType.getArtTypeName());

		WebArticle notice = new WebArticle();
		notice.setArtTypeId(typeId);
		notice.setArtIsPublish("是");
		
		pageVo.setSort("art_sort");
		pageVo.setOrder("asc");
		List<WebArticle> dataList = webArticleService.findInPage(notice, pageVo);
		int total = webArticleService.findTotal(notice);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList, total);
	}
	
	/**
	 *  @Description 首页最新 广告,
		@author :heqiuyun
		@date2017年5月25日
	 */
	@RequestMapping(value = "/getShouYeListByType")
	@ResponseBody
	public AjaxResult getShouYeListByType(Long typeId) {
		WebArticleType artType = webArticleTypeService.findById(typeId);
		WebUtil.getRequest().setAttribute("typeName", artType.getArtTypeName());

		WebArticle notice = new WebArticle();
		notice.setArtTypeId(typeId);
		notice.setArtIsPublish("是");
		List<WebArticle> dataList = webArticleService.findSYInPage(notice);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList,null);
	}
	
	/**
	 * 打开公告详情
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/toNewsForm")
	public String noticeForm(Long id) {
		WebArticle notice;
		if (id == null) {
			throw new GlobleException("此公告不存在！");
		}
		notice = webArticleService.findById(id);
		WebUtil.getRequest().setAttribute("obj", notice);

		WebArticleType artType = webArticleTypeService.findById(notice
				.getArtTypeId());
		WebUtil.getRequest().setAttribute("typeName", artType.getArtTypeName());
		
		return "admin/lin/news-form";
	}
}